/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.file.entity;

import java.io.File;
import java.io.Serializable;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.utils.DateUtils;
import cn.net.ecode.common.utils.FileUtils;
import cn.net.ecode.common.utils.StringUtils;

/**
 * 上传文件配置参数Entity
 * @author jiangwenchao、ThinkGem
 * @version 2015-6-13
 */
public class FileParms implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String bizCategory; 			// 业务类型 用来区分附件相关联业务表
	private String bizKey; 					// 业务key
	private String uploadType = "file"; 	// 上传文件类型（image、media、file）
	private String readonly = "false"; 		// 查看模式
	private String multiFile = "true";		// 是否上传多文件
	private Integer imageMaxWidth = -1;		// 最大宽度(uploadType为images生效)
	private Integer imageMaxHeight = -1;	// 最大高度(uploadType为images生效)
	private String allowServerSelect = "false";	// 是否允许从服务器上选
	private Integer maxUploadNum = -1;			// 多文件下允许最多上传几个 -1 为不限制
	private String lazyUpload = "false";		// 选择后直接上传还是提交表单时上传

	private String relativePath; 	// 相对路径
	private String absolutePath; 	// 绝对路径
	private Integer maxFileSize;	// 上传单个文件最大字节
	private Integer maxFilesSize;	// 一次上传多个文件的总大小限制（只有在lazyUpload=true时有效
	
	private String allowMimes;		// -1所有文件类型，不过滤
	private String notAllowMimes;	// -1不允许上传的mime
	private String thumbnailFormat;	// 缩略图规格

	private String allowSuffixs;	// 后缀文件过滤（允许）
	private String imageAllowSuffixs;	// 图片后缀文件过滤（允许）
	private String mediaAllowSuffixs;	// 视频媒体后缀文件过滤（允许）
	private String fileAllowSuffixs;	// 文件后缀文件过滤（允许）

	private String fileId;
	private String fileMd5;
	private String fileUserId;
	private String separator = File.separator;	// 文件分割符

	public static final String FILE_TYPE_IMAGE = "image"; 	// 图片
	public static final String FILE_TYPE_MEDIA = "media"; 	// 媒体
	public static final String FILE_TYPE_FILE = "file"; 	// 文档
	
	public static final String FILEUPLOAD_BASE_URL = "$fileupload/"; 	// 文档
	
	public FileParms() {
		super();
		
		// 上传用户为  $webupload ，前面加$号为了与系统用户名区分，防止重复。
		String uploadPath = Global.getConfig("file.uploadPath");
		if (StringUtils.isBlank(uploadPath)){
			uploadPath = Global.getConfig("uploadPath", "{yyyy}{MM}/");
		}
		
		// 路径中的规则变量替换（目前支持时间format）
		String[] strs = StringUtils.substringsBetween(uploadPath, "{", "}");
		if (strs != null){
			for (String str : strs){
				if (StringUtils.inString(str, "yyyy", "MM", "dd", "HH", "mm", "ss", "E")){
					uploadPath = StringUtils.replace(uploadPath, "{"+str+"}", DateUtils.getDate(str));
				}
			}
		}
		
		// 文件相对路径
		relativePath = FileUtils.path(uploadPath); 
		
		// 文件绝对路径
		absolutePath = Global.getUserfilesBaseDir(FILEUPLOAD_BASE_URL + uploadPath + File.separator); 
		
		// 上传单个文件最大字节
		String maxFileSizes = Global.getConfig("file.maxFileSize");
		if (StringUtils.isBlank(maxFileSizes)){
			maxFileSizes = Global.getConfig("maxFileSize");
		}
		if (StringUtils.isNotBlank(maxFileSizes)) {
			String[] sz = StringUtils.split(maxFileSizes, "*");
			maxFileSize = 1;
			for (String s : sz) {
				maxFileSize = maxFileSize * Integer.valueOf(s);
			}
		}else{
			maxFileSize = 0;
		}
		if (maxFileSize <= 1) {
			maxFileSize = 5 * 1024 * 1024; // 5M
		}
		
		// 获取上传最大文件大小限制
		String maxFilesSizes = Global.getConfig("file.maxFilesSize");
		if (StringUtils.isBlank(maxFilesSizes)){
			maxFilesSizes = Global.getConfig("maxFilesSize");
		}
		if (StringUtils.isNotBlank(maxFilesSizes)) {
			String[] sz = StringUtils.split(maxFilesSizes, "*");
			maxFilesSize = 1;
			for (String s : sz) {
				maxFilesSize = maxFilesSize * Integer.valueOf(s);
			}
		}else{
			maxFilesSize = 0;
		}
		if (maxFilesSize <= 1) {
			maxFilesSize = 100 * 1024 * 1024; // 100M
		}
		
		// 允许的文件格式
		allowMimes = Global.getConfig("file.allowMimes");
		if (StringUtils.isBlank(allowMimes)){
			allowMimes = Global.getConfig("allowMimes", "-1");
		}
		// 不允许的文件格式
		notAllowMimes = Global.getConfig("file.notAllowMimes");
		if (StringUtils.isBlank(notAllowMimes)){
			notAllowMimes = Global.getConfig("notAllowMimes", "-1");
		}
		// 缩略图大小格式
		thumbnailFormat = Global.getConfig("file.thumbnailFormat");
		if (StringUtils.isBlank(thumbnailFormat)){
			thumbnailFormat = Global.getConfig("thumbnailFormat", "40x40");
		}
		
		// 允许的全部文件后缀
		allowSuffixs = StringUtils.EMPTY;
		
		// 允许的图片文件后缀
		imageAllowSuffixs = Global.getConfig("file.imageAllowSuffixs");
		if (StringUtils.isBlank(imageAllowSuffixs)){
			imageAllowSuffixs = Global.getConfig("imageAllowSuffixs", "-1");
		}
		if (!"-1".equals(imageAllowSuffixs)){
			allowSuffixs += imageAllowSuffixs;
			if (!allowSuffixs.endsWith(",")){
				allowSuffixs += ",";
			}
		}

		// 允许的媒体文件后缀
		mediaAllowSuffixs = Global.getConfig("file.mediaAllowSuffixs");
		if (StringUtils.isBlank(mediaAllowSuffixs)){
			mediaAllowSuffixs = Global.getConfig("mediaAllowSuffixs", "-1");
		}
		if (!"-1".equals(mediaAllowSuffixs)){
			allowSuffixs += mediaAllowSuffixs;
			if (!allowSuffixs.endsWith(",")){
				allowSuffixs += ",";
			}
		}

		// 允许的文档文件后缀
		fileAllowSuffixs = Global.getConfig("file.fileAllowSuffixs");
		if (StringUtils.isBlank(fileAllowSuffixs)){
			fileAllowSuffixs = Global.getConfig("fileAllowSuffixs", "-1");
		}
		if (!"-1".equals(fileAllowSuffixs)){
			allowSuffixs += fileAllowSuffixs;
			if (!allowSuffixs.endsWith(",")){
				allowSuffixs += ",";
			}
		}
	}

	public String getBizCategory() {
		return bizCategory;
	}

	public void setBizCategory(String bizCategory) {
		this.bizCategory = bizCategory;
	}

	public String getBizKey() {
		return bizKey;
	}

	public void setBizKey(String bizKey) {
		this.bizKey = bizKey;
	}

	public String getUploadType() {
		return uploadType;
	}

	public void setUploadType(String uploadType) {
		this.uploadType = uploadType;
	}

	public String getReadonly() {
		return readonly;
	}

	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}

	public String getMultiFile() {
		return multiFile;
	}

	public void setMultiFile(String multiFile) {
		this.multiFile = multiFile;
	}

	public Integer getImageMaxWidth() {
		return imageMaxWidth;
	}

	public void setImageMaxWidth(Integer imageMaxWidth) {
		this.imageMaxWidth = imageMaxWidth;
	}

	public Integer getImageMaxHeight() {
		return imageMaxHeight;
	}

	public void setImageMaxHeight(Integer imageMaxHeight) {
		this.imageMaxHeight = imageMaxHeight;
	}

	public String getAllowServerSelect() {
		return allowServerSelect;
	}

	public void setAllowServerSelect(String allowServerSelect) {
		this.allowServerSelect = allowServerSelect;
	}

	public Integer getMaxUploadNum() {
		return maxUploadNum;
	}

	public void setMaxUploadNum(Integer maxUploadNum) {
		this.maxUploadNum = maxUploadNum;
	}

	public String getLazyUpload() {
		return lazyUpload;
	}

	public void setLazyUpload(String lazyUpload) {
		this.lazyUpload = lazyUpload;
	}

//	public String getUploadPath() {
//		return uploadPath;
//	}
//
//	public void setUploadPath(String uploadPath) {
//		this.uploadPath = uploadPath;
//	}

	public Integer getMaxFileSize() {
		return maxFileSize;
	}

	public void setMaxFileSize(Integer maxFileSize) {
		this.maxFileSize = maxFileSize;
	}

	public Integer getMaxFilesSize() {
		return maxFilesSize;
	}

	public void setMaxFilesSize(Integer maxFilesSize) {
		this.maxFilesSize = maxFilesSize;
	}

	public String getAllowSuffixs() {
		return allowSuffixs;
	}

	public void setAllowSuffixs(String allowSuffixs) {
		this.allowSuffixs = allowSuffixs;
	}

	public String getAllowMimes() {
		return allowMimes;
	}

	public void setAllowMimes(String allowMimes) {
		this.allowMimes = allowMimes;
	}

	public String getNotAllowMimes() {
		return notAllowMimes;
	}

	public void setNotAllowMimes(String notAllowMimes) {
		this.notAllowMimes = notAllowMimes;
	}

	public String getThumbnailFormat() {
		return thumbnailFormat;
	}

	public void setThumbnailFormat(String thumbnailFormat) {
		this.thumbnailFormat = thumbnailFormat;
	}

	public String getImageAllowSuffixs() {
		return imageAllowSuffixs;
	}

	public void setImageAllowSuffixs(String imageAllowSuffixs) {
		this.imageAllowSuffixs = imageAllowSuffixs;
	}

	public String getMediaAllowSuffixs() {
		return mediaAllowSuffixs;
	}

	public void setMediaAllowSuffixs(String mediaAllowSuffixs) {
		this.mediaAllowSuffixs = mediaAllowSuffixs;
	}

	public String getFileAllowSuffixs() {
		return fileAllowSuffixs;
	}

	public void setFileAllowSuffixs(String fileAllowSuffixs) {
		this.fileAllowSuffixs = fileAllowSuffixs;
	}

	public String getRelativePath() {
		return relativePath;
	}

	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}

	public String getAbsolutePath() {
		return absolutePath;
	}

	public void setAbsolutePath(String absolutePath) {
		this.absolutePath = absolutePath;
	}

	public String getFileMd5() {
		return fileMd5;
	}

	public void setFileMd5(String fileMd5) {
		this.fileMd5 = fileMd5;
	}

	public String getSeparator() {
		return separator;
	}

	public void setSeparator(String separator) {
		this.separator = separator;
	}

	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public String getFileUserId() {
		return fileUserId;
	}

	public void setFileUserId(String fileUserId) {
		this.fileUserId = fileUserId;
	}
	
	/**
	 * 获取绝对路径
	 * @param relativePath
	 * @return
	 */
	public String getAbsolutePathByRel(String uploadPath) {
		return Global.getUserfilesBaseDir(FILEUPLOAD_BASE_URL + uploadPath + File.separator);
	}
	
}